package com.xuecheng.content.config;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.FileUtil;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.errors.MinioException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

@Component
public class MinioTemplate {

    // 声明MIME类型常量
    public static final String TEXT_HTML = "text/html";
    public static final String IMAGE_JPG = "image/jpg";

    @Autowired
    private MinioProperties prop;

    private static MinioClient minioClient;

    // 初始化
    @PostConstruct
    public void init() {
        minioClient = MinioClient
                .builder()
                .credentials(prop.getAccessKey(), prop.getSecretKey())
                .endpoint(prop.getEndpoint())
                .build();
    }

    // 文件上传
    public String upload(InputStream inputStream, String filename) {
        try {
            // 处理上传文件的文件名称
            String filePath = "images/" + DateTime.now().toString("yyyy/MM/dd/") + UUID.randomUUID().toString() + "." + FileUtil.extName(filename);

            // 构造上传的参数对象PutObjectArgs
            PutObjectArgs args = PutObjectArgs.builder()
                    .bucket(prop.getBucket()) // 指定桶
                    .contentType(IMAGE_JPG) // 指定MIME类型
                    .object(filePath) // 指定文件名
                    .stream(inputStream, inputStream.available(), -1) // 指定文件流
                    .build();
            // 上传
            minioClient.putObject(args);

            // 获取文件的访问地址
            String imageUrl = prop.getDomain() + "/" + prop.getBucket() + "/" + filePath;
            // 返回
            return imageUrl;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //文件删除
    public  void removeObject(String name) {
        try {
            // 从mybucket中删除myobject。
            minioClient.removeObject(prop.getBucket(),name );
            System.out.println("删除成功");
        } catch (MinioException e) {
            System.out.println("Error: " + e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }
    }
    // 抽取文件上传方法（html页面资源）
    public String uploadHtml(InputStream inputStream, String filename) { // as123123abc.jpg
        try {
            // 配置上传的请求地址  /pages/47.html
            String filePath = "pages/"+filename;

            // 完成文件上传
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .object(filePath) // 文件名
                    .contentType(TEXT_HTML) // 文件类型
                    .bucket(prop.getBucket()) // 桶名
                    .stream(inputStream, inputStream.available(), -1)  //参数1:文件流  参数2:文件大小  参数3: 分片大小
                    .build();
            minioClient.putObject(putObjectArgs);

            // 返回文件名
            String urlPath = prop.getDomain() + "/" + prop.getBucket() + filePath;

            return urlPath;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}